package top.leyi.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @desc 为了提升性能，采用异步io
 *        单例模式
 */
@Component
public class AsyncStorageData {


        private volatile static AsyncStorageData asyncStorageData = null;

        public static AsyncStorageData getInstance(){

            if(asyncStorageData == null){
                synchronized (AsyncStorageData.class){
                    if(asyncStorageData == null){
                        asyncStorageData = new AsyncStorageData();
                    }
                }
            }

            return asyncStorageData;
        }


        private   static  final int NTHREADS = 10;
        private   static  final ExecutorService service = Executors.newFixedThreadPool(NTHREADS);

         /**
          *  代理模式
          */
        public void exec(Runnable runnable){
                service.execute(runnable);
        }

        public void exec(Callable task) throws  Exception{
                  service.submit(task);
        }

    }
